package per.ace.fastjson.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.web.bind.annotation.*;
import per.ace.fastjson.entity.User;

import java.util.*;

/**
 * @program: springbootDemo
 * @description: test
 * @author: ace
 * @Date: 2020-01-03 09:51
 **/
@RestController
@RequestMapping("test")
public class TestController {

    @GetMapping("t1")
    public String test1() {
        Map jsonMap = new HashMap<String, Object>();
        jsonMap.put("name", "tony");
        jsonMap.put("age", 16);
        jsonMap.put("class", null);
        List l = new ArrayList<>();
        l.add("鲁B88888");
        jsonMap.put("car",l);
        return JSONObject.toJSONString(jsonMap);
    }

    @GetMapping("t2")
    public String test2() {
        Map jsonMap = new HashMap<String, Object>();
        jsonMap.put("name", "tony");
        jsonMap.put("age", 16);
        jsonMap.put("class", null);
        return JSONObject.toJSONString(jsonMap, SerializerFeature.WriteNullStringAsEmpty);
    }

    @GetMapping("t3")
    public Map test3() {
        Map jsonMap = new HashMap<String, Object>();
        jsonMap.put("name", "tony");
        jsonMap.put("age", 16);
        jsonMap.put("class", null);
        return jsonMap;
    }

    @GetMapping("t4")
    public Map test4() {
        Map jsonMap = new HashMap<String, Object>();
        jsonMap.put("name", "tony");
        jsonMap.put("age", 16);
        jsonMap.put("class", null);
        return jsonMap;
    }

    @GetMapping("t5")
    public Map test5() {
        Map jsonMap = new HashMap<String, Object>();
        jsonMap.put("name", "tony");
        jsonMap.put("date", new Date());
        return jsonMap;
    }

    @PostMapping("t6")
    public Map test6(@RequestBody Map RequestBody) {
        String str = JSONObject.toJSONString(RequestBody);
        System.out.println(str);
        Map jsonMap = new HashMap<String, Object>();
        jsonMap.put("name", "tony");
        jsonMap.put("date", new Date());
        return jsonMap;
    }

    @GetMapping("t7")
    public User test7() {
       User u = new User();
       u.setName("tony");
       u.setCreateDate(new Date());
       u.setCreateBy(44L);
       return u;
    }

    @PostMapping("t8")
    public User test8(@RequestBody User user) {
        System.out.println(user.getCreateDate());
        String str = JSONObject.toJSONString(user);
        System.out.println(str);
        return user;
    }
}
